﻿<template>
	<div class="etQuestion-container">
		<el-dialog v-model="isShowDialog" :width="800" draggable="" :close-on-click-modal="false">
			<template #header>
				<div style="color: #fff">
					<!--<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit /> </el-icon>-->
					<span>{{ props.title }}</span>
				</div>
			</template>
			<el-form :model="ruleForm" ref="ruleFormRef" label-width="auto" :rules="rules">
				<el-row :gutter="35">
					<el-form-item v-show="false">
						<el-input v-model="ruleForm.id" />
					</el-form-item>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="排序" prop="orderNo">
							<el-input-number v-model="ruleForm.orderNo" placeholder="请输入排序" clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="状态" prop="status">
							<el-switch v-model="ruleForm.status" active-text="是" inactive-text="否" />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="题目分类" prop="categoryId">
							<el-input v-model="ruleForm.categoryId" placeholder="请输入题目分类" maxlength="19" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="题目内容" prop="content">
							<editor v-model="ruleForm.content" :defaultConfig="editorConfig" style="height: 300px;" />
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="题目类型" prop="questionType">
							<el-input v-model="ruleForm.questionType" placeholder="请输入题目类型" maxlength="10" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="选项" prop="options" v-if="[2,3].includes(ruleForm.questionType)">
							<option-editor v-model="ruleForm.options" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="答案" prop="answer">
							<answer-editor 
								v-model="ruleForm.answer"
								:question-type="ruleForm.questionType"
								:options="ruleForm.options"
							/>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="解析" prop="analysis">
							<el-input v-model="ruleForm.analysis" placeholder="请输入解析" maxlength="2000" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="难度" prop="difficulty">
							<el-input v-model="ruleForm.difficulty" placeholder="请输入难度" maxlength="10" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="分数" prop="score">
							<el-input-number v-model="ruleForm.score" placeholder="请输入分数" clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="标签" prop="tags">
							<el-input v-model="ruleForm.tags" placeholder="请输入标签" maxlength="500" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="时间限制（秒）" prop="timeLimit">
							<el-input-number v-model="ruleForm.timeLimit" placeholder="请输入时间限制（秒）" clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="附件" prop="attachments">
							<el-upload
							list-type="picture-card"
							:show-file-list="false"
							:http-request="uploadAttachmentsHandle">
								<img
								v-if="ruleForm.attachments"
								:src="ruleForm.attachments"
								@click="ruleForm.attachments=''"
								style="width: 100%; height: 100%; object-fit: contain"/>
								<el-icon v-else><Plus /></el-icon>
								
							</el-upload>
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="出题人备注" prop="creatorComments">
							<el-input v-model="ruleForm.creatorComments" placeholder="请输入出题人备注" maxlength="1000" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="跳转地址ID" prop="jumpAddressId">
							<el-input v-model="ruleForm.jumpAddressId" placeholder="请输入跳转地址ID" maxlength="19" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="跳转地址路径" prop="jumpAddressPath">
							<el-input v-model="ruleForm.jumpAddressPath" placeholder="请输入跳转地址路径" maxlength="512" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel">取 消</el-button>
					<el-button type="primary" @click="submit">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>
<style lang="scss" scoped>
:deep(.el-select),
:deep(.el-input-number) {
	width: 100%;
}
</style>
<script lang="ts" setup>
	import { ref,onMounted } from "vue";
	import { ElMessage } from "element-plus";
	import type { FormRules } from "element-plus";
	import { getDictDataItem as di, getDictDataList as dl } from '/@/utils/dict-utils';
  	import { formatDate } from '/@/utils/formatTime';
    import { Plus } from "@element-plus/icons-vue";
    import { UploadRequestOptions } from "element-plus";
    import {uploadAttachments} from '/@/api/main/et-question';
	import { addEtQuestion, updateEtQuestion, detailEtQuestion } from "/@/api/main/et-question";
	import { Editor } from '@wangeditor/editor-for-vue';
	import OptionEditor from './optionEditor.vue';
	import AnswerEditor from './answerEditor.vue';

	//父级传递来的参数
	var props = defineProps({
		title: {
		type: String,
		default: "",
	},
	});
	//父级传递来的函数，用于回调
	const emit = defineEmits(["reloadTable"]);
	const ruleFormRef = ref();
	const isShowDialog = ref(false);
	const ruleForm = ref<any>({});
	//自行添加其他规则
	const rules = ref<FormRules>({
		orderNo: [{required: true, message: '请输入排序！', trigger: 'blur',},],
		questionBankId: [{required: true, message: '请选择题库！', trigger: 'change',},],
		categoryId: [{required: true, message: '请输入题目分类！', trigger: 'blur',},],
		content: [{required: true, message: '请输入题目内容！', trigger: 'blur',},],
		questionType: [{required: true, message: '请输入题目类型！', trigger: 'blur',},],
		difficulty: [{required: true, message: '请输入难度！', trigger: 'blur',},],
	});

	// 页面加载时
	onMounted(() => {

	});

	// 打开弹窗
	const openDialog = async (row: any) => {
		// ruleForm.value = JSON.parse(JSON.stringify(row));
		// 改用detail获取最新数据来编辑
		let rowData = JSON.parse(JSON.stringify(row));
		if (rowData.id)
			ruleForm.value = (await detailEtQuestion(rowData.id)).data.result;
		else
			ruleForm.value = rowData;
		isShowDialog.value = true;
	};

	// 关闭弹窗
	const closeDialog = () => {
		emit("reloadTable");
		isShowDialog.value = false;
	};

	// 取消
	const cancel = () => {
		isShowDialog.value = false;
	};

	// 提交
	const submit = async () => {
		ruleFormRef.value.validate(async (isValid: boolean, fields?: any) => {
			if (isValid) {
				let values = ruleForm.value;
				if (ruleForm.value.id == undefined || ruleForm.value.id == null || ruleForm.value.id == "" || ruleForm.value.id == 0) {
					await addEtQuestion(values);
				} else {
					await updateEtQuestion(values);
				}
				closeDialog();
			} else {
				ElMessage({
					message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
					type: "error",
				});
			}
		});
	};

	const uploadAttachmentsHandle = async (options: UploadRequestOptions) => {
		const res = await uploadAttachments(options);
		ruleForm.value.attachments = res.data.result?.url;
	};

	// 编辑器配置
	const editorConfig = {
		placeholder: '请输入题目内容',
		MENU_CONF: {}
	};

	//将属性或者函数暴露给父组件
	defineExpose({ openDialog });
</script>




